package org.his.controller.basicdata.zhiliaoyi;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.his.dao.InstrInfoDao;
import org.his.pojo.zhiliaoyi.InstrInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/ins")
public class InstrlnfoController {

    @Autowired
    private InstrInfoDao instrInfoDao;

    @RequestMapping("/index")
    public String index() throws Exception{
        return "basicdata/zhiliaoyi/index";
    }


    @RequestMapping("/form")
    public String form() throws Exception{
        return "basicdata/zhiliaoyi/form";
    }

    @RequestMapping("/editform")
    public String editform() throws Exception{
        return "basicdata/zhiliaoyi/editform";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, InstrInfo condition) throws Exception{
        Map<String, Object> map = new HashMap<>();
        Page<InstrInfo> pager = PageHelper.startPage(page, limit);

        map.put("data", instrInfoDao.getList(condition));
        map.put("count", pager.getTotal());
        map.put("code", 0);
        map.put("msg", null);
        return map;
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String,Object> batchDelete(Integer ids) throws Exception{
        Map<String,Object> map = new HashMap<>();
        instrInfoDao.delete(ids);
        return map;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(InstrInfo  instrInfo) throws Exception{
        Map<String, Object> map = new HashMap<>();

        if (instrInfo.getInstrStatus() == null){
            instrInfo.setInstrStatus(0);
        }

        instrInfoDao.add(instrInfo);
        return map;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(InstrInfo  instrInfo) throws Exception{
        Map<String, Object> map = new HashMap<>();

        if (instrInfo.getInstrStatus() == null){
            instrInfo.setInstrStatus(0);
        }

        instrInfoDao.update(instrInfo);

        return map;
    }


    @RequestMapping("/zhiliaoyitupian")
    @ResponseBody
    public Map<String,Object> zhiliaoyitupian(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception{
        String pathRoot = request.getServletContext().getRealPath("/assets/images/zhiliaoyi");
        Map<String,Object> map = new HashMap<>();
        try {
            if(!file.isEmpty()){
                String filename = System.currentTimeMillis()+"_"+file.getOriginalFilename();//时间戳加源文件名
                File dest = new File(pathRoot + File.separator + filename);
                file.transferTo(dest);
                map.put("msg","ok");
                map.put("imgPath","assets/images/zhiliaoyi/"+filename);
            }
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg","error");
        } catch (IllegalStateException e) {
            e.printStackTrace();
            map.put("msg","error");
        }
        return map;
    }

    @RequestMapping("/get")
    @ResponseBody
    public InstrInfo get(Integer instrID)throws Exception{
        return instrInfoDao.get(instrID);
    }

}
